﻿using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SmallBusinessKit.DataAccess.Repositories;
using SmallBusinessKit.Models.Entities;

namespace SmallBusinessKit.Tests.Repositories
{
    /// <summary>
    /// Unit Tests for XmlPersonRepository Logic
    /// </summary>
    [TestClass]
    public class XmlPersonRepositoryTests : UnitTestBase
    {
        #region Properties

        private XmlPersonRepository _xmlRep;

        #endregion Properties

        #region Tests

        [TestInitialize]
        public override void Setup()
        {
            base.Setup();

            var people = XmlData.ParsePeople(); // to use data from file to save time...

            people.ElementAt(1).IsVisible = false;    // to test GetMany(i => i.IsVisible)
            people.ElementAt(3).IsVisible = false;    // to test GetMany(i => i.IsVisible)

            _xmlRep    = new XmlPersonRepository(people);
        }

        [TestMethod]
        public void XmlPersonGetById()
        {
            // Act
            var result = _xmlRep.GetById("02");

            // Assert
            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(Person));
            Assert.AreEqual("02", result.Id);
        }

        [TestMethod]
        public void XmlPersonGet()
        {
            // Act
            var result = _xmlRep.Get(i => i.Id == "02");

            // Assert
            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(Person));
            Assert.AreEqual("02", result.Id);
        }

        [TestMethod]
        public void XmlPersonGetMany()
        {
            // Act
            var results = _xmlRep.GetMany(i => i.IsVisible).ToList();

            // Assert
            Assert.IsNotNull(results);
            Assert.IsInstanceOfType(results, typeof (IEnumerable<Person>));
            Assert.AreEqual(10, results.Count());
        }

        [TestMethod]
        public void XmlPersonGetAll()
        {
            // Act
            var results = _xmlRep.GetAll().ToList();

            // Assert
            Assert.IsNotNull(results);
            Assert.AreEqual(12, results.Count());
            Assert.IsInstanceOfType(results, typeof(IEnumerable<Person>));
        }

        [TestMethod]
        public void XmlPersonGetAsQueryable()
        {
            // Act
            var result = _xmlRep.GetAsQueryable();

            // Assert
            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(IQueryable<Person>));
        }

        #endregion
    }
}
